package com.example.musicserver;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

public class Test {

    public static void main(String[] args) {
        // TXT文件路径
        String txtFilePath = "path/to/your/file.txt";
        // Excel文件输出路径
        String excelFilePath = "path/to/your/output.xlsx";

        convertTxtToExcel(txtFilePath, excelFilePath);
    }

    public static void convertTxtToExcel(String txtFilePath, String excelFilePath) {
        Workbook workbook = new XSSFWorkbook(); // 创建Excel工作簿
        Sheet sheet = workbook.createSheet("Data"); // 创建工作表

        try (BufferedReader br = new BufferedReader(new FileReader(txtFilePath))) {
            String line;
            int rowNum = 0;
            while ((line = br.readLine()) != null) {
                Row row = sheet.createRow(rowNum++); // 创建行
                String[] values = line.split(","); // 假设TXT文件是逗号分隔的
                for (int i = 0; i < values.length; i++) {
                    Cell cell = row.createCell(i); // 创建单元格
                    cell.setCellValue(values[i]); // 设置单元格的值
                }
            }

            // 写入Excel文件
            try (FileOutputStream fileOut = new FileOutputStream(excelFilePath)) {
                workbook.write(fileOut);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close(); // 关闭工作簿
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
